package com.zmn.plat.dubbo.impl.file;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.apache.dubbo.config.annotation.Service;

import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.business.interfaces.pic.PicUploadBService;
import com.zmn.plat.dubbo.interfaces.file.FileUploadRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;

/**
 * 文件上传
 * 
 * @author: LuJia
 * @version: v1.0
 * @since: 2020/05/22 19:48
 **/
@Service(loadbalance = "roundrobin", timeout = 10000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class FileUploadRemoteServiceImpl implements FileUploadRemoteService {

    @Resource
    PicUploadBService picUploadBService;

    @Override
    public ResponseDTO<String> uploadFile(@NotBlank String directory, @NotBlank String fileName, @NotNull byte[] bytes) {

        InputStream inputStream = new ByteArrayInputStream(bytes);

        ResultDTO resultDTO = picUploadBService.uploadFile(directory, fileName, inputStream);

        if (resultDTO.getStatusBool()) {
            String picUrl = picUploadBService.getFileUrl(directory, fileName);
            return ResponseDTO.success(picUrl);
        }

        return ResponseDTO.fail(resultDTO.getMessage());
    }
}
